package com.feisuanyz.service.impl;

import com.feisuanyz.dao.MealSelectionMapper;
import com.feisuanyz.pojo.domain.MealSelectionDO;
import com.feisuanyz.pojo.dto.MealSelectionDTO;
import com.feisuanyz.pojo.query.MealSelectionQuery;
import com.feisuanyz.pojo.vo.PageResult;
import com.feisuanyz.service.MealSelectionService;
import java.lang.Boolean;
import java.lang.Long;
import java.lang.Override;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.feisuanyz.exception.BusinessException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.Map;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.feisuanyz.constant.ResultCodeConstant;
import java.util.Calendar;

/**
 * 历史记录管理的实现
 *
 * @author Chenke Zhao
 * @date 2025-03-06 10:18:05
 */
@Service
public class MealSelectionServiceImpl implements MealSelectionService {

    @Autowired
    private MealSelectionMapper mealSelectionMapper;

    @Override
    public PageResult<MealSelectionDO> getMealSelectionRecords(MealSelectionQuery query) {
        IPage<MealSelectionDO> page = new Page<>(query.getPageIndex(), query.getPageSize());
        QueryWrapper<MealSelectionDO> wrapper = Wrappers.query();
        wrapper.eq("user_id", query.getUserId());
        IPage<MealSelectionDO> resultPage = mealSelectionMapper.selectPage(page, wrapper);
        PageResult<MealSelectionDO> pageResult = new PageResult<>(resultPage);
        return pageResult;
    }

    @Override
    public Boolean addMealSelection(MealSelectionDTO mealSelectionDTO) {
        QueryWrapper<MealSelectionDO> wrapper = Wrappers.query();
        wrapper.eq("user_id", mealSelectionDTO.getUserId()).eq("meal_id", mealSelectionDTO.getMealId()).apply("selection_time >= {0}", getDateBeforeThreeDays());
        MealSelectionDO existingRecord = mealSelectionMapper.selectOne(wrapper);
        if (existingRecord != null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        MealSelectionDO mealSelectionDO = new MealSelectionDO();
        mealSelectionDO.setUserId(mealSelectionDTO.getUserId());
        mealSelectionDO.setMealId(mealSelectionDTO.getMealId());
        mealSelectionMapper.insert(mealSelectionDO);
        return true;
    }

    @Override
    public Boolean updateMealSelection(MealSelectionDTO mealSelectionDTO) {
        MealSelectionDO mealSelectionDO = new MealSelectionDO();
        mealSelectionDO.setId(Math.toIntExact(mealSelectionDTO.getSelectionId()));
        mealSelectionDO.setMealId(mealSelectionDTO.getMealId());
        mealSelectionMapper.updateById(mealSelectionDO);
        return true;
    }

    @Override
    public Boolean deleteMealSelection(Long selectionId) {
        mealSelectionMapper.deleteById(selectionId);
        return true;
    }

    private Date getDateBeforeThreeDays() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, -3);
        return calendar.getTime();
    }
}
